package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.UUID;

/**
 * @ProjectName duolaimall
 * @Descriptiom TODO
 * @Aurhor: 蔡文强
 * @Since: 2022/10/26 9:44
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {


    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;


    /**
     * 创建订单
     * @param request
     * @return
     */
    @RequestMapping(value = "/order",method = RequestMethod.POST)
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        // 传进来的userId不准确？？？
        String user_info = servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(user_info);
        long uid = Long.parseLong(jsonObject.get("uid").toString());
        request.setUserId(uid);

        // 设置UniqueKey  这个项目里没用到这个
        request.setUniqueKey(UUID.randomUUID().toString());

        String orderId = orderCoreService.createOrder(request);

        return new ResponseUtil<>().setData(orderId);
    }

    /**
     * 查询当前用户的历史订单列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/order",method = RequestMethod.GET)
    public ResponseData order(OrderListRequest orderListRequest,HttpServletRequest servletRequest){
        String user_info = servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(user_info);
        long uid = Long.parseLong(jsonObject.get("uid").toString());
        orderListRequest.setUserId(uid);

        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);

        return new ResponseUtil<>().setData(orderListResponse);
    }

    /**
     * 查询订单详情
     * @return
     */
    @RequestMapping(value = "/order/{id}",method = RequestMethod.GET)
    public ResponseData orderDetail(@PathVariable("id") String orderId){

        OrderDetailResponse orderDetailResponse =  orderQueryService.orderDetail(orderId);

        return new ResponseUtil<>().setData(orderDetailResponse);
    }

    /**
     * 取消订单
     * @return
     */
    @RequestMapping(value = "/cancelOrder",method = RequestMethod.POST)
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest){

        orderCoreService.cancelOrder(cancelOrderRequest.getOrderId());

        return new ResponseUtil<>().setData("成功","success");
    }


    /**
     *
     * @param id
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 小焦
     * @since 2022/10/26 下午 09:46
     */

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id){
        boolean deleteOrder = orderCoreService.deleteOrder(id);
        if(deleteOrder==true){
            return new ResponseUtil().setData("成功");
        }
        else {
            //    ORDER_DELETE_FALSE("504", "交易未完成,不能删除");
            return new ResponseUtil<>().setErrorMsg(504, "交易未完成,不能删除");
        }
    }
}
